﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using UniversityStudentManagemnetSystem.DAO.DAL;
using UniversityStudentManagemnetSystem.DAO.Gateway;

namespace UniversityStudentManagemnetSystem.BLL
{
    public class StudentResultManager
    {
        private StudentCourseEnrollGateway enrollGateway = new StudentCourseEnrollGateway();
        ResultGateway resultGateway=new ResultGateway();
        public Student SearchStudentByRegNo(string regNo)
        {
            Student aStudent = enrollGateway.SearchStudentByRegNo(regNo);
            return aStudent;
        }


        public DataTable GetAllEnrollCourseForCurrentStudent(string regNo)
        {
            DataTable courseTabel = resultGateway.GetAllEnrollCoursesForRegNo(regNo);
            return courseTabel;
        }

        public string SaveStudentResult(Student aStudent)
        {
            string message;
            if (aStudent.RegNo=="")
            {
                message = "RegNo Missing";
            }
            else if(resultGateway.IsUploadResultForthisCourse(aStudent))
            {
                message = "This Course:\t(" + aStudent.Course.Code +")"+ "\nResult Already Uploaed To " + aStudent.Name;
            }
            else
            {
                resultGateway.SaveResult(aStudent);
                message = "Save Successfull\n"+"Name:\t"+aStudent.Name +"\nCourse Code:\t"+aStudent.Course.Code+"\nResult:\t"+aStudent.Grade; 
            }
           
            return message;
        }



        public Student SearchStudentResultByRegNo(string regNo)
        {
            Student aStudent = resultGateway.SearchStudentResultByRegNo(regNo);
            return aStudent;
        }


       //New Section


        public string GetAllEnrollCourses(string selectedRegNo)
        {
            DataTable table= resultGateway.GetAllEnrollCoursesForSelectedRegNo(selectedRegNo);
            return table.Rows.Count.ToString();

        }

        public string GetAllCompletedCourse(string selectedRegNo)
        {
            DataTable table = resultGateway.GetAllCompletedCourseForSelectedRegNo(selectedRegNo);
            return table.Rows.Count.ToString();
        }

        public string GetAllRemainingCourses(string selectedRegNo)
        {
            int remainingCourses = Convert.ToInt32(GetAllEnrollCourses(selectedRegNo)) - Convert.ToInt32(GetAllCompletedCourse(selectedRegNo));
            return remainingCourses.ToString();
        }

        public string TotalCreditForEnrollCourses(string selectedRegNo)
        {
            return resultGateway.GetTotalCreditOfEnrolledCoursesForSelectedRegNo(selectedRegNo);
        }

        public string GetAllCompletedCredit(string selectedRegNo)
        {
            return resultGateway.GetAllCompletedCreditForSelectedRegNo(selectedRegNo);
        }

        public string GetAllRemainingCredit(string selectedRegNo)
        {
            int remainingCredit = Convert.ToInt32(TotalCreditForEnrollCourses(selectedRegNo)) - Convert.ToInt32(GetAllCompletedCredit(selectedRegNo));
            return remainingCredit.ToString();
        }

        public string GetCurrentCgpa(string selectedRegNo)
        {
            double totalCgpa= Convert.ToDouble(resultGateway.GetCgpaForSelectedRegNo(selectedRegNo));
            double cgpa = totalCgpa / Convert.ToInt32(GetAllCompletedCourse(selectedRegNo));
            return cgpa.ToString();
        }

        public string GetCurrentGradeLetter(string selectedRegNo)
        {
            string gradeLetter = null;
            double currentCgpa = Convert.ToDouble(GetCurrentCgpa(selectedRegNo));
            if (currentCgpa >= 4.00)
            {
                gradeLetter = "A+";
            }
            else if (currentCgpa < 4 && currentCgpa >= 3.75)
            {
                gradeLetter = "A";
            }
            else if (currentCgpa < 3.75 && currentCgpa >= 3.50)
            {
                gradeLetter = "A-";
            }
            else if (currentCgpa < 3.50 && currentCgpa >= 3.25)
            {
                gradeLetter = "B+";
            }
            else if (currentCgpa < 3.25 && currentCgpa >= 3.00)
            {
                gradeLetter = "B";
            }
            else if (currentCgpa < 3.00 && currentCgpa >= 2.75)
            {
                gradeLetter = "B-";
            }
            else if (currentCgpa < 2.75 && currentCgpa >= 2.50)
            {
                gradeLetter = "C+";
            }
            else if (currentCgpa < 2.50 && currentCgpa >= 2.25)
            {
                gradeLetter = "C";
            }
            else if (currentCgpa < 2.25 && currentCgpa >= 2.00)
            {
                gradeLetter = "C-";
            }
            else if (currentCgpa < 2.00)
            {
                gradeLetter = "F";
            }
            return gradeLetter;
        }

        public DataTable GetComplitedCourses(string selectedRegNo)
        {
            DataTable table = resultGateway.GetAllCompletedCourseForSelectedRegNo(selectedRegNo);
            return table;
        }

        public string CertificateInformation(string selectedRegNo)
        {
            int totalEnrolledCourses = Convert.ToInt32(GetAllEnrollCourses(selectedRegNo));
            int completeCourses = Convert.ToInt32(GetAllCompletedCourse(selectedRegNo));
            string msg = "";
            if (totalEnrolledCourses==completeCourses)
            {
                msg = "You are eligible to get the certificate";
            }
            else
            {
                msg = "You are not eligible to get the certificate";
            }
            return msg;
        }
    }
}